import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

void main() {
  runApp(MyApp());
}

//自定义组件
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // MaterialApp是根组件
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('Dialog Web'),
          ),
          body: DialogPage()),
      theme: ThemeData(primaryColor: Colors.blue),
    );
  }
}

class DialogPage extends StatefulWidget {
  DialogPage({Key? key}) : super(key: key);

  @override
  _DialogPageState createState() => _DialogPageState();
}

//builder的context参数是build(BuildContext context)闭包传入
class _DialogPageState extends State<DialogPage> {
  void _alertDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text("提示信息"),
            actions: [
              ElevatedButton(
                  onPressed: () {
                    print("cancel");
                    Navigator.pop(context);
                  },
                  child: Text("cancel")),
              ElevatedButton(
                  onPressed: () {
                    print("ok");
                    Navigator.pop(context);
                  },
                  child: Text("ok"))
            ],
          );
        });
  }

  void _simpleDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("simple dialog"),
            children: [
              SimpleDialogOption(
                child: Text('optiona A'),
                onPressed: () {
                  print('a');
                  Navigator.pop(context);
                },
              ),
              SimpleDialogOption(
                child: Text('optiona B'),
                onPressed: () {
                  print('b');
                  Navigator.pop(context);
                },
              )
            ],
          );
        });
  }

  _modelBottomSheet() async {
    var resoult = await showModalBottomSheet(
        context: context,
        builder: (context) {
          return Container(
            height: 200,
            child: Column(
              children: [
                ListTile(
                  title: Text("分享A"),
                  onTap: () {
                    Navigator.pop(context, "分享A");
                  },
                ),
                ListTile(
                    title: Text("分享B"),
                    onTap: () {
                      Navigator.pop(context, "分享B");
                    })
              ],
            ),
          );
        });
    print(resoult);
  }

  _toast() {
    Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Row(
            children: [
              ElevatedButton(
                child: Text("alert dialog"),
                onPressed: _alertDialog,
              ),
              ElevatedButton(
                child: Text("simple dialog"),
                onPressed: _simpleDialog,
              ),
              ElevatedButton(
                child: Text("bottom sheet"),
                onPressed: _modelBottomSheet,
              ),
              SizedBox(
                height: 24.0,
              ),
              ElevatedButton(
                onPressed: _toast,
                child: Text("Flutter Toast Context"),
              )
            ],
          )
        ],
      ),
    );
  }
}
